<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta content="IE=edge" http-equiv="X-UA-Compatible"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title>Document</title>
    <!-- 引入样式 -->
    <link href="../../plugins/element-ui/index.css" rel="stylesheet"/>
    <link href="../../styles/common.css" rel="stylesheet"/>
    <link href="../../styles/page.css" rel="stylesheet"/>
</head>
<body>
<div id="owner-add-app" class="addBrand-container">
    <div class="container">
        <el-form
                ref="ruleForm"
                :inline="false"
                :model="ruleForm"
                :rules="rules"
                class="demo-ruleForm"
                label-width="180px"
        >
            <el-form-item label="业主姓名:" prop="name">
                <el-input
                        v-model="ruleForm.name"
                        maxlength="20"
                        placeholder="请输入业主姓名"
                />
            </el-form-item>
            <el-form-item label="工作单位:" prop="companyName">
                <el-input
                        v-model="ruleForm.companyName"
                        maxlength="20"
                        placeholder="请输入工作单位"
                />
            </el-form-item>
            <el-form-item label="手机号:" prop="phone">
                <el-input
                        v-model="ruleForm.phone"
                        maxlength="20"
                        placeholder="请输入手机号"
                />
            </el-form-item>
            <div class="subBox address">
                <el-form-item>
                    <el-button @click="goBack()"> 取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm', false)">
                        保存
                    </el-button>
                    <el-button
                            v-if="actionType == 'add'"
                            class="continue"
                            type="primary"
                            @click="submitForm('ruleForm', true)"
                    >
                        保存并继续添加
                    </el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/owner.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script>
    new Vue({
        el: "#owner-add-app",
        data() {
            return {
                id: "",
                actionType: "",
                ruleForm: {
                    name: "",
                    companyName: "",
                    phone: "",
                },
            };
        },
        computed: {
            //校验规则
            rules() {
                return {
                    //姓名
                    name: [{required: true, validator: checkName, trigger: "blur"}],
                    companyName: [{required: true, trigger: "blur"}],
                    phone: [
                        {required: true, validator: checkPhone, trigger: "blur"},
                    ],
                };
            },
        },
        created() {
            this.id = requestUrlParam("id");
            this.actionType = this.id ? "edit" : "add";
            if (this.id) {
                this.init();
            }
        },
        mounted() {
        },
        methods: {
            async init() {
                queryOwnerById(this.id).then((res) => {
                    console.log(res);
                    if (String(res.code) === "1") {
                        console.log(res.data);
                        this.ruleForm = res.data;
                    } else {
                        this.$message.error(res.msg || "操作失败");
                    }
                });
            },
            submitForm(formName, st) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.actionType === "add") {
                            const params = {
                                ...this.ruleForm,
                            };
                            addOwner(params)
                                .then((res) => {
                                    if (res.code === 1) {
                                        this.$message.success("业主添加成功！");
                                        if (!st) {
                                            this.goBack();
                                        } else {
                                            this.ruleForm = {
                                                name: "",
                                                roomNumber: "",
                                                area: "",
                                                companyName: "",
                                                phone: "",
                                            };
                                        }
                                    } else {
                                        this.$message.error(res.msg || "操作失败");
                                    }
                                })
                                .catch((err) => {
                                    this.$message.error("请求出错了：" + err);
                                });
                        } else {
                            const params = {
                                ...this.ruleForm,
                            };
                            editOwner(params)
                                .then((res) => {
                                    if (res.code === 1) {
                                        this.$message.success("业主信息修改成功！");
                                        this.goBack();
                                    } else {
                                        this.$message.error(res.msg || "操作失败");
                                    }
                                })
                                .catch((err) => {
                                    this.$message.error("请求出错了：" + err);
                                });
                        }
                    } else {
                        console.log("error submit!!");
                        return false;
                    }
                });
            },
            goBack() {
                window.parent.menuHandle(
                    {
                        id: "3",
                        url: "/backend/page/owner/list.html",
                        name: "业主管理",
                    },
                    false
                );
            },
        },
    });
</script>
</body>
</html>
